/*
 * Created on 2004-5-26 To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

package cn.com.yinhex.as.util;

import javax.servlet.jsp.JspWriter;

/**
 * @author test To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ServerUtil
{
	public static final long DIFF_TIMESTAMP = 1060500000000L;
	private static long l = 0L;

	/**
	 * 用输入的字符串创建一个id号
	 * 
	 * @param str
	 * @return
	 */
	public static String getid(String str)
	{
		return str + getid();
	}

	/**
	 * 创建8位字符的id号
	 * 
	 * @return
	 */
	public static String getid()
	{
		try
		{
			String id = IntUtil.c10to32(getLongID() - DIFF_TIMESTAMP);
			while (id.length() < 8)
			{
				id = "0" + id;
			}
			return id;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 创建13位数字型的id号
	 * @return
	 */
	public static long getLongID()
	{
		long longtime = System.currentTimeMillis();
		if (longtime > l)
		{
			l = longtime;
		}
		else
		{
			l++;
		}
		return l;
	}

	/**
	 * 用输入的字符串和时间戳创建id号
	 * 
	 * @param str
	 * @param unixtime
	 * @return
	 */
	public static String getid(String str, long longtime)
	{
		String id = IntUtil.c10to32(longtime - DIFF_TIMESTAMP);
		while (id.length() < 8)
		{
			id = "0" + id;
		}
		return str + id;
	}

	/**
	 * 从id取得时间
	 * 
	 * @param id
	 * @return
	 */
	public static long getTime(String id)
	{
		try
		{
			return IntUtil.c32to10(id) + DIFF_TIMESTAMP;
		}
		catch (Exception e)
		{
			return (long) 0;
		}
	}

	/**
	 * 把id加一
	 * @param id
	 * @return
	 */
	public static String increaseID(String id)
	{
		id = id.toUpperCase();
		String id1 = id.substring(0, id.length() - 1);
		String id2 = id.substring(id.length() - 1);

		try
		{
			byte b = id2.getBytes("ISO8859-1")[0];
			java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(4);
			bb.putInt(b + 1);
			id2 = new String(bb.array()).trim();
		}
		catch (Exception e)
		{}
		return id1 + id2;
	}

	/**
	 * 把id减一
	 * @param id
	 * @return
	 */
	public static String decreaseID(String id)
	{
		id = id.toUpperCase();
		String id1 = id.substring(0, id.length() - 1);
		String id2 = id.substring(id.length() - 1);

		try
		{
			byte b = id2.getBytes("ISO8859-1")[0];
			java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(4);
			bb.putInt(b - 1);
			id2 = new String(bb.array()).trim();
		}
		catch (Exception e)
		{}
		return id1 + id2;
	}

	public static void alert(String str, JspWriter out)
	{
		try
		{
			out.println("<script>");
			out.println("alert('" + str + "');");
			out.println("</script>");
		}
		catch (Exception e)
		{}
	}

	public static void closeWindow(JspWriter out)
	{
		try
		{
			out.println("<script>");
			out.println("window.close();");
			out.println("</script>");
		}
		catch (Exception e)
		{}
	}
}